//
//  ZYEmotionKeyboard.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/7/4.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit



class ZYEmotionKeyboard: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    //Users/muyanche/Code/Swift/03/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/Classes/Compose/Views/Emotion/Emoticons.bundle/Contents/Resources/emoji/info.plist
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        //添加到控价
        addSubview(toolbar)
        addSubview(pageView)
        addSubview(pageControl)
        
        //添加约束
        toolbar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
            
        }
        
        //添加约束
        pageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(toolbar.snp_top)
            
        }
        
        //添加约束
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pageView)
            make.centerX.equalTo(pageView)
            
        }
        
        //接收toolbar中哪个按钮的点击
        toolbar.toolbarClosure = {[weak self](type: ZYEmotionToolbarType) -> () in
            
            //7.5 因为下面已经可以判断滚动到哪个位置了,所以下面的这些就不需要了
//            switch type{
//            case .Default:
//                print("默认表情")
//            case .Emoji:
//                print("Emoji表情")
//            case .Lxl:
//                print("浪下浪")
//            }
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            //防止循环引用
            
            //让配图滚动到指定的位置,UICollectionViewScrollPosition.  屏幕里面的cell在滚动之后是靠近左边还是右边
            
            self?.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Right, animated: false)
            
            //总的页数..7.7...
            self?.pageControl.numberOfPages = ZYEmotionTools.sharedTools.allEmotion[indexPath.section].count
            //当前的页数
            self?.pageControl.currentPage = 0
            
            
        }
        
    }
    
    //懒加载控件
    private lazy var toolbar:ZYEmotionToolbar = ZYEmotionToolbar()
    
    //表情键盘
    private lazy var pageView: ZYEmotionPageView = {
        
        let view = ZYEmotionPageView()
        
        //------------7.5------设置代理,判断哪个cell显示的多
        //设置短裤
        view.delegate = self
        
        //取消弹簧的效果
        view.bounces = false
        //分页
        view.pagingEnabled = true
        //隐藏滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        
        return view

    
    }()
    
    //7.7 懒加载pageControl
    private lazy var pageControl: UIPageControl = {
        
        let view = UIPageControl()
        view.numberOfPages = ZYEmotionTools.sharedTools.allEmotion[0].count
        
            
        view.currentPage = 0
        //颜色
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
//        //选中的颜色
//        view.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
//        
        //通过KVO赋值
        
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return view
    
    }()
    
    
}

//MARK: - 实现代理方法

extension ZYEmotionKeyboard: UICollectionViewDelegate {
    
    /*
        ---计算cell的
        -- 当前pageView界面上顶多展示两个cell给用户看
        --相对于pageView 的左侧作为起点是0点,屏幕中心 x = 屏幕的宽度 / 2 + contentOffset.x 的偏移量
        -- 由于cell贴在pageView上面,所以每个cell都有自己的frame
        -- 如果能够判断出来哪个cell的frame包含屏幕的中心点,就代表它显示的多
        -- 通过cell和pageView可以得到indexpath
        -- 通过indexPath 可以得到section
        -- section的 0 1 2 viewWithTag 通过该方法可以得到button,是button的状态发生改变

    */
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //print(scrollView.contentOffset.x)
        
        let centerX = SCREENW / 2 + scrollView.contentOffset.x
        
        //获取当前屏幕显示的cell
        let cellArray = pageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
            
        }
        
        //判断只有是两个cell的时候才显示
        if cellArray.count == 2 {
        
            //获取第一个cell
            let firstCell = cellArray.first!
            //获取第二个cell
            let secondCell = cellArray.last!
            
            //获取显示多的frame的cell 的indexPath
            var indexPath:NSIndexPath
            
            //判断一下中心点是否子啊第一个cell中
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                
                indexPath = pageView.indexPathForCell(firstCell)!
                //print("1多")

            } else {
                indexPath = pageView.indexPathForCell(secondCell)!
                //print("2多")

            }
            
            print(indexPath.section)
            
            //关键的是为了滚动pageView来改变toolBarButton的状态
            toolbar.setCurrentButtons(indexPath.section)
            
            //总的页数
            pageControl.numberOfPages = ZYEmotionTools.sharedTools.allEmotion[indexPath.section].count
            
            
            //当前的页数 7.7
            
            pageControl.currentPage = indexPath.item
            
        }
    
    }

}








